package com.example.hosinject.slice;

import com.example.hosinject.ResourceTable;
import com.example.hosinject.inject.InjectUtils;
import com.example.hosinject.inject.SliceInjectView;
import com.example.hosinject.inject.SliceInjectViewClick;
import com.example.hosinject.util.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.RoundProgressBar;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.dispatcher.task.TaskPriority;

import java.text.NumberFormat;

public class MainAbilitySlice extends AbilitySlice {

    @SliceInjectView(ResourceTable.Id_roundProgressBar)
    RoundProgressBar roundProgressBar;

    @SliceInjectView(ResourceTable.Id_horizontalProgressBar)
    ProgressBar horizontalProgressBar;

    int max = 100;

    boolean progressing;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        InjectUtils.inject(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @SliceInjectViewClick({ResourceTable.Id_toast, ResourceTable.Id_dialog, ResourceTable.Id_showRound, ResourceTable.Id_showHorizontal})
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_toast:
                ToastUtils.createToastDialog(getContext(), "this is a toast", LayoutAlignment.BOTTOM).show();
                break;
            case ResourceTable.Id_dialog:
                new ToastDialog(getContext())
                        .setTitleText("dialog")
                        .setContentText("this is a dialog")
                        .show();
                break;

            case ResourceTable.Id_showHorizontal:
                toggleProgressBar(horizontalProgressBar);
                break;

            case ResourceTable.Id_showRound:
                toggleProgressBar(roundProgressBar);
                break;
        }
    }

    private void toggleProgressBar(ProgressBar progressBar){
        if (progressing) {
            return;
        }
        if (progressBar.getVisibility() == Component.INVISIBLE) {
            progressBar.setVisibility(Component.VISIBLE);
            getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> {
                for (int i = 0; i < max; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int finalI = i + 1;
                    getUITaskDispatcher().syncDispatch(() -> {
                        NumberFormat numberFormat = NumberFormat.getInstance();
                        numberFormat.setMaximumFractionDigits(2); //设置精确到小数点后2位
                        String result = numberFormat.format((float) (finalI) / max * 100); //所占百分比
                        progressBar.setProgressHintText(result + "%");
                        progressBar.setProgressValue(finalI);
                    });
                }
                progressing = false;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getUITaskDispatcher().syncDispatch(() -> progressBar.setVisibility(Component.INVISIBLE));
            });

        } else {
            progressing = false;
            progressBar.setVisibility(Component.INVISIBLE);
        }
    }
}
